package rule

import (
	"regexp"
	"strconv"
)

func init() {
	Register(&mobileRule{reg: regexp.MustCompile(`^1[3456789]{1}\d{9}$`)})
}

type mobileRule struct {
	reg *regexp.Regexp
}

func (m *mobileRule) Name() string {
	return "mobile"
}

func (m *mobileRule) Check(in *RuleInput) (err error) {
	if in.Value != nil {
		var mobile string
		switch in.Value.(type) {
		case int:
			mobile = strconv.FormatInt(int64(in.Value.(int)), 10)
		case int64:
			mobile = strconv.FormatInt(in.Value.(int64), 10)
		case uint:
			mobile = strconv.FormatUint(uint64(in.Value.(uint)), 10)
		case uint64:
			mobile = strconv.FormatUint(in.Value.(uint64), 10)
		case string:
			mobile = in.Value.(string)
		}
		if mobile != "" && len(mobile) == 11 && m.reg.FindStringIndex(mobile) != nil {
			return
		}
	}
	return in.Error()
}
